/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using DaveSexton.DocProject.Wizards.Foundation;
using System.Windows.Forms;
using DaveSexton.DocProject.Configuration;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject.Wizards
{
	public static class DocProjectConfigurationWizard
	{
		#region Methods
		public static bool Run(IDocProject project, EventHandler<ExceptionEventArgs> errorHandler)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			DocProjectOptions.SetDocProjectVersion(project, DocProjectEnvironment.DocProjectVersion);

			ChooseBuildEngineStep chooseEngine = new ChooseBuildEngineStep(project);
			ChooseSourcesStep chooseSources = new ChooseSourcesStep(project);
			CreateDocSiteCredentialsStep createCreds = (project.IsDocSite) ? new CreateDocSiteCredentialsStep(project) : null;

			WizardStep completionChain = (createCreds == null) ? chooseSources : createCreds.InsertBefore(chooseSources);

			string name = project.IsDocSite ? "DocSite" : "DocProject";

			using (WizardForm form = new WizardForm(string.Format(System.Globalization.CultureInfo.CurrentCulture,
				WizardText.NewProjectWizardName, name), chooseEngine, completionChain))
			{
				form.Error += errorHandler;
				form.WizardCanceled += Canceled;

				form.Icon = (project.IsDocSite)
					? (project.IsVisualBasic)
						? Icons.VBDocSite
						: Icons.CSharpDocSite
					: (project.IsVisualBasic)
						? Icons.VBDocProject
						: Icons.CSharpDocProject;

				if (form.ShowDialog(DocProjectEnvironment.MainWindow) == DialogResult.OK)
				{
					project.Options.Commit();
					project.Save();

					if (DocProjectEnvironment.RunningInVisualStudio)
						BuildController.Refresh();

					project.Provider.NotifyNewProjectWizardCompleted(project, form.Controller, true);
					return true;
				}
				else
				{
					project.Provider.NotifyNewProjectWizardCompleted(project, form.Controller, false);
					return false;
				}
			}
		}

		private static void Canceled(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = MessageBox.Show(DocProjectEnvironment.MainWindow,
				string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Text.ConfirmCancelWizard, Environment.NewLine),
				Resources.Text.ConfirmCancelWizardTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2,
				DocProjectEnvironment.MessageBoxOptions) == DialogResult.No;
		}
		#endregion
	}
}
